<template>
	<view>
		<u-sticky>
			<u-tabs :list="list" :is-scroll="false" :current="current" @change="sectionChange"></u-tabs>
		</u-sticky>
		<view class=" u-p-r-30 u-p-l-30 u-m-t-20">
			<u-search placeholder="请输入关键词搜索" :clearable="true" @input="onInputChange" border-color="#606266"
				@custom="search" @search="search"></u-search>
		</view>
		<u-card v-if="current == 0">
			<view class="" slot="head">
				<view class="wrap">
					<u-row>
						<u-col span="5" text-align="left">
							<view class=" weikaishi">
								<span>商品名称</span>
							</view>
						</u-col>
						<u-col span="4" text-align="left">
							<view class=" weikaishi">
								<span>现库位</span>
							</view>
						</u-col>
						<u-col span="3" text-align="left">
							<view class=" weikaishi">
								<span>商品编号</span>
							</view>
						</u-col>
					</u-row>
				</view>
			</view>
			<view class="" slot="body">
				<u-row v-for="(item,index) in dataList" :key="index" @click="receive(item)">
					<u-col span="5" text-align="left">
						<uni-badge size="small" text="加急" absolute="rightTop" v-if="item.taskFlag == '急'">
							<view class="lanse u-m-t-10 huanhang weikaishi2">
								<span>{{item.goodsName}}</span>
							</view>
						</uni-badge>
						<uni-badge size="small" text="强下" absolute="rightTop" v-else-if="item.taskFlag == '强'">
							<view class="lanse u-m-t-10 huanhang weikaishi2">
								<span>{{item.goodsName}}</span>
							</view>
						</uni-badge>
						<view class="lanse u-m-t-10 huanhang weikaishi2" v-else>
							<span>{{item.goodsName}}</span>
						</view>
					</u-col>
					<u-col span="4" text-align="left">
						<view class="u-m-t-10 huanhang weikaishi2">
							<span>{{item.oldPositionNumber}}</span>
						</view>
					</u-col>
					<u-col span="3" text-align="left">
						<view class="u-m-t-10 huanhang weikaishi3">
							<span>{{item.goodsNumber}}</span>
						</view>
					</u-col>
					<u-row>
						<u-col span="12" text-align="center">
							<view class="jiange">
							</view>
						</u-col>
					</u-row>
				</u-row>
			</view>
		</u-card>
		<u-card v-if="current == 1">
			<view class="" slot="head">
				<view class="wrap">
					<u-row>
						<u-col span="5" text-align="left">
							<view class=" weikaishi">
								<span>商品名称</span>
							</view>
						</u-col>
						<u-col span="4" text-align="left">
							<view class=" weikaishi">
								<span>现库位</span>
							</view>
						</u-col>
						<u-col span="3" text-align="left">
							<view class=" weikaishi">
								<span>领取人</span>
							</view>
						</u-col>
					</u-row>
				</view>
			</view>
			<view class="" slot="body">
				<u-row v-for="(item,index) in swipelist" :key="index" @click="toput(item.id,item.offUserId)">
					<u-col span="5" text-align="left">
						<view class="lanse u-m-t-10 huanhang weikaishi2">
							<span>{{item.goodsName}}</span>
						</view>
					</u-col>
					<u-col span="4" text-align="left">
						<view class="u-m-t-10 huanhang weikaishi2">
							<span>{{item.oldPositionNumber}}</span>
						</view>
					</u-col>
					<u-col span="3" text-align="left">
						<view class="u-m-t-10 huanhang weikaishi2">
							<span>{{item.offUserName}}</span>
						</view>
					</u-col>
					<u-col span="12" text-align="center">
						<view class="jiange">
						</view>
					</u-col>
				</u-row>
			</view>
		</u-card>
		<view>
			<u-toast ref="uToast" />
		</view>
	</view>
</template>

<script>
	// 防抖函数定义
	function debounce(func, wait) {
		let timeout;
		return function() {
			const context = this,
				args = arguments;
			clearTimeout(timeout);
			timeout = setTimeout(function() {
				func.apply(context, args);
			}, wait);
		};
	}
	export default {
		data() {
			return {
				flex: '',
				keyword: '',
				list: [{
						name: '待领取'
					},
					{
						name: '已领取'
					}
				],
				current: 0,
				dataList: [],
				swipelist: [],

				pageNo: 1, //第几页
				hasMore: true, //是否有下一页
				orderCount: 0, //总共多少条
			}
		},
		onLoad() {
			//#ifdef H5 || APP-PLUS
			this.flex = 'u-flex'
			//#endif
		},
		onShow() {
			this.refresh()
		},
		onPullDownRefresh() {
			this.refresh()
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 500)
		},
		onReachBottom() {
			if (this.current == 0) {
				uni.showLoading({
					title: '加载中'
				})
				if (this.hasMore) {
					this.pageNo++
					this.loadOrder({
						condition: this.keyword,
						current: this.current
					}).then(res => {})
				} else {
					setTimeout(() => {
						uni.hideLoading()
						uni.showToast({
							icon: 'none',
							title: '已经到低了哦',
							duration: 2000
						});
					}, 500)
				}
			}
		},
		methods: {
			toput(id, offUserId) {
				this.$http.post('/rest/hxStorageOffShelf/findUser', {}, {
					params: {
						offUserId: offUserId
					},
					header: {
						'Authorization': uni.getStorageSync('token'),
					}
				}).then(res => {
					if (res.data.code == 200) {
						this.$Router.push({
							name: 'hxOffShelfTask_task',
							params: {
								id: id
							},
						})
					} else {
						this.$refs.uToast.show({
							title: res.data.message,
							type: 'error'
						})
						setTimeout(() => {
							this.refresh()
						}, 1000)
					}
				})
			},
			receive(item) {
				uni.showModal({
					title: '确认领取吗？',
					content: '商品:' + item.goodsName + '，编号:' + item.goodsNumber,
					success: (res) => {
						if (res.confirm) {
							this.$http.post('/rest/hxStorageOffShelf/receive', {}, {
								params: {
									id: item.id
								},
								header: {
									'Authorization': uni.getStorageSync('token'),
								}
							}).then(res => {
								if (res.data.code == 200) {
									this.$refs.uToast.show({
										title: '领取成功',
										type: 'success'
									})
									this.$Router.push({
										name: 'hxOffShelfTask_task',
										params: {
											id: item.id
										},
									})
								} else {
									this.$refs.uToast.show({
										title: res.data.message,
										type: 'error'
									})
									setTimeout(() => {
										this.refresh()
									}, 1000)
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			refresh() {
				uni.showLoading({
					title: '加载中...'
				})
				if (this.current == 0) {
					this.pageNo = 1
					this.hasMore = true
					this.loadOrder({
						condition: this.keyword,
						current: this.current
					}).then(res => {})
				} else if (this.current == 1) {
					this.$http.post('/rest/hxStorageOffShelf/findOffShelfList', {}, {
						params: {
							condition: this.keyword,
							current: this.current
						},
						header: {
							'Authorization': uni.getStorageSync('token'),
						}
					}).then(res => {
						uni.hideLoading()
						this.list[1].name = '已领取(' + res.data.data.length + ')'
						let newswipelist = []
						for (let item1 of res.data.data) {
							var arr = {}
							arr.id = item1.id
							arr.offUserId = item1.offUserId
							arr.goodsName = item1.goodsName
							arr.oldPositionNumber = item1.oldPositionNumber
							arr.offUserName = item1.offUserName
							arr.show = false
							newswipelist.push(arr)
						}
						this.swipelist = newswipelist
					})
				}
			},
			loadOrder(param = {}) {
				return new Promise((resolve, reject) => {
					param = Object.assign(param, {
						page: this.pageNo,
						limit: 15
					})
					this.$http.post('/rest/hxStorageOffShelf/findOffShelfListPage', {}, {
						params: param,
						header: {
							'Authorization': uni.getStorageSync('token'),
						}
					}).then(res => {
						uni.hideLoading()
						if (this.pageNo == 1) {
							this.dataList = res.data.data
						} else {
							this.dataList = this.dataList.concat(res.data.data)
						}
						this.orderCount = res.data.count
						this.list[0].name = '待领取(' + res.data.count + ')'
						this.hasMore = this.pageNo * 15 < res.data.count
						resolve(res)
					})
				})
			},
			sectionChange(e) {
				this.current = e
				this.refresh()
			},
			search(e) {
				this.refresh()
			},
			onInputChange(value) {
				this.keyword = value;
			},
			debouncedUpdateKeyword: debounce(function(value) {
				this.keyword = value;
				// console.log('更新关键词:', this.keyword);
			}, 300)
		}
	}
</script>


<style lang="scss">
	.u-body-item {
		padding: 20rpx 15rpx;
	}

	.wrap {
		padding: 0rpx 12rpx;
	}

	.u-row {
		margin: 10rpx 2rpx;
	}

	.weikaishi2 {
		font-size: 34rpx;
	}

	.weikaishi3 {
		font-size: 26rpx;
	}

	.jiange {
		border-bottom: 1px solid #ebebeb;
	}
</style>